import Api from '@/api/index.js'
import Utils from '@/common/utils.js'
export default {
  data () {
    return {
      loading: false,
      searchPlaceholder: '请输入心仪的院校',
      keyWord: '',
      // 查询相关信息
      levelResult: [],
      levelDefaultIds: [],
      levelIds: [],
      districtResult: [],
      districtDefaultIds: [],
      districtIds: [],
      departmentResult: [],
      departmentDefaultIds: [],
      departmentIds: [],
      // 查询结果
      schoolData: [],
      // 学科专业搜索提示
      zyListData: [],
      results: []
    }
  },
  methods: {
    // 获取初始数据
    getInitData (qType) {
      let that = this
      that.levelResult = Utils.session('levelResult') ? Utils.session('levelResult') : ''
      that.districtResult = Utils.session('districtResult') ? Utils.session('districtResult') : ''
      // that.departmentResult = Utils.session('departmentResult') ? Utils.session('departmentResult') : ''
      that.levelDefaultIds = []
      that.districtDefaultIds = []
      that.departmentDefaultIds = []

      if ([9,10].includes(qType)) {
        that.searchPlaceholder = '请输入心仪的专业'
        that.getDept(qType, {majorRank: true})
      } else if (qType === 8) {
        that.getDept(qType, {which: 'GZGZ'})
      } else if (qType === 7) {
        that.getDept(qType, {which: 'MBYX'})
      } else if (qType === 6) {
        that.getDept(qType, {which: 'DLYX'})
      } else if (qType === 5) {
        that.getDept(qType, {which: 'XYH'})
      } else if (qType === 4) {
        that.getDept(qType, {which: 'WS'})
      } else if (qType === 3) {
        that.getDept(qType, {which: 'RK'})
      } else if (qType === 2) {
        that.getDept(qType, {which: 'QJP'})
      } else {
        that.getDept(qType)
      }
    },
    getData (qType) {
      let that = this
      if ([2, 3, 4, 5, 9,10].indexOf(qType) >= 0) {
        that.levelResult = [{
          id: '985',
          levelName: '985院校'
        },
        {
          id: '211',
          levelName: '211院校'
        },
        {
          id: 'syl',
          levelName: '双一流院校'
        },
        {
          id: 'not',
          levelName: '普通本科'
        }]
        that.levelDefaultIds = ['985', '211', 'syl', 'not']
      } else if (that.levelResult.length) {
        that.levelResult = typeof that.levelResult === 'string' ? JSON.parse(that.levelResult) : that.levelResult
        that.levelResult.map((item, idx) => {
          that.levelDefaultIds.push(item.id)
        })
      }
      if (that.districtResult.length) {
        that.districtResult = typeof that.districtResult === 'string' ? JSON.parse(that.districtResult) : that.districtResult
        that.districtResult.map((item, idx) => {
          that.districtDefaultIds.push(item.id)
        })
      }
      if (that.departmentResult.length) {
        that.departmentResult = typeof that.departmentResult === 'string' ? JSON.parse(that.departmentResult) : that.departmentResult
        that.departmentResult.map((item, idx) => {
          that.departmentDefaultIds.push(item)
        })
      }
      if (qType === 1) {
        that.getCollegeHomepage({})
      } else {
        that.getCollegeData(qType)
      }
    },
    // 获取主管部门
    getDept (qType, qsData = null) {
      let that = this
      Api.getDepartment(qsData).then(res => {
        if (res) {
          that.departmentResult = res
          that.getData(qType)
        } else {
          console.log('获取主管部门失败')
        }
      })
    },
    // 获取查询大学信息
    getCollegeData (qType) {
      let that = this
      let qsData = {}
      this.$vux.loading.show()
      qsData.department = that.departmentIds.length > 0 ? that.departmentIds.join(',') : that.departmentDefaultIds.join(',')
      qsData.areaId = that.districtIds.length > 0 ? that.districtIds.join(',') : that.districtDefaultIds.join(',')
      qsData.collegesLevelId = that.levelIds.length > 0 ? that.levelIds.join(',') : that.levelDefaultIds.join(',')
      if (that.keyWord) {
        qsData.name = that.keyWord
      }
      qType = parseInt(qType)
      if (qType === 1) {
        that.getCollegeHomepage(qsData)
      } else if ([2, 3, 4, 5].indexOf(qType) >= 0) {
        let whichList = ['QJP', 'RK', 'WS', 'XYH']
        that.getCollegeBKRank({
          department: qsData.department,
          level: qsData.collegesLevelId,
          areaId: qsData.areaId,
          which: whichList[[2, 3, 4, 5].indexOf(qType)],
          name: qsData.name
        })
      } else if (qType === 6) {
        that.getCollegeIndepRank({
          department: qsData.department,
          areaId: qsData.areaId,
          name: qsData.name
        })
      } else if (qType === 7) {
        that.getCollegeMBRank({
          department: qsData.department,
          areaId: qsData.areaId,
          name: qsData.name
        })
      } else if (qType === 8) {
        that.getCollegeGZRank({
          department: qsData.department,
          areaId: qsData.areaId,
          name: qsData.name
        })
      } else if (qType === 9) {
        that.getCollegeZYRank({
          department: qsData.department,
          level: qsData.collegesLevelId,
          areaId: qsData.areaId,
          name: qsData.name
        })
        that.getCollegeZyRankZy()
      }else if (qType === 10) {
        that.getCollegeRKZYRank({
          department: qsData.department,
          level: qsData.collegesLevelId,
          areaId: qsData.areaId,
          name: qsData.name,
          which:'RKXK'
        })
        that.getCollegeZyRankZy()
      }
    },
    // 获取院校网站详情
    getCollegeHomepage (qsData) {
      let that = this
      this.$vux.loading.show()
      Api.getCollegeHomepage(qsData).then(res => {
        that.schoolData = res
        this.$vux.loading.hide()
      })
    },
    // 获取本科院校排行榜
    getCollegeBKRank (qsData) {
      let that = this
      this.$vux.loading.show()
      Api.getCollegeBKRank(qsData).then(res => {
        that.schoolData = res
        this.$vux.loading.hide()
      })
    },
    // 获取独立院校100强
    getCollegeIndepRank (qsData) {
      let that = this
      this.$vux.loading.show()
      Api.getCollegeIndepRank(qsData).then(res => {
        that.schoolData = res
        this.$vux.loading.hide()
      })
    },
    // 获取民办院校100强
    getCollegeMBRank (qsData) {
      let that = this
      this.$vux.loading.show()
      Api.getCollegeMBRank(qsData).then(res => {
        that.schoolData = res
        this.$vux.loading.hide()
      })
    },
    // 获取高职高专院校排行榜
    getCollegeGZRank (qsData) {
      let that = this
      this.$vux.loading.show()
      Api.getCollegeGZRank(qsData).then(res => {
        that.schoolData = res
        this.$vux.loading.hide()
      })
    },
    // 获取学科专业排行榜
    getCollegeZYRank (qsData) {
      let that = this
      this.$vux.loading.show()
      Api.getCollegeZYRank(qsData).then(res => {
        that.schoolData = res
        // that.getResult()
        this.$vux.loading.hide()
      })
    },
    // 获取学科专业排行榜
    getCollegeRKZYRank (qsData) {
      let that = this
      this.$vux.loading.show()
      Api.getCollegeRKZYRank(qsData).then(res => {
        that.schoolData = res
        // that.getResult()
        this.$vux.loading.hide()
      })
    },
    // 截取院校名称
    interceptName (uName) {
      let idx = uName.indexOf('（')
      return idx > 0 ? uName.substr(0, idx) : uName
    },
    // 设置排名颜色
    htmlToRankType (college) {
      let _rtHtml = ''
      if (college.isToo && college.isToo !== '0') {
        _rtHtml += '<span class="ranktype sl2">211</span>'
      }
      if (college.isNef && college.isNef !== '0') {
        _rtHtml += '<span class="ranktype sl1">985</span>'
      }
      if (college.isSyl && college.isSyl !== '0') {
        _rtHtml += '<span class="ranktype sl3">双一流</span>'
      }
      if (college.type === '公办') {
        _rtHtml += '<span class="ranktype sl4">公办</span>'
      }
      if (college.type === '民办学校') {
        _rtHtml += '<span class="ranktype sl6">民办</span>'
      }
      if (college.type === '独立学院') {
        _rtHtml += '<span class="ranktype sl5">独立</span>'
      }
      if (college.type === '中外合作办学') {
        _rtHtml += '<span class="ranktype sl7">中外</span>'
      }

      if (college.level === '高职') {
        _rtHtml += '<span class="ranktype sl8">高职</span>'
      } else if (college.level === '高职高专') {
        _rtHtml += '<span class="ranktype sl8">高职</span>'
      } else if (college.level === '独立') {
        _rtHtml += '<span class="ranktype sl5">独立</span>'
      } else if (college.level === '民办') {
        _rtHtml += '<span class="ranktype sl6">民办</span>'
      } else {
        _rtHtml += '<span class="ranktype sl9">本科</span>'
      }
      return _rtHtml
    },
    // 访问学校连接
    routerLink (link) {
      window.open(link)
    },
    // 条件筛选
    changeFilter (type, fData, checkList) {
      this.$refs.popupFilter.setFilterData(type, fData, checkList)
    },
    onSubmit () {
      this.getCollegeData(this.queryType)
    },
    onCancel () {
      this.keyWord = ''
      this.getCollegeData(this.queryType)
    },
    // 筛选结束
    checkDone (type, checkList) {
      if (type === 1) {
        this.districtIds = checkList
      } else if (type === 2) {
        this.levelIds = checkList
      } else if (type === 3) {
        this.departmentIds = checkList
      }
      this.getCollegeData(this.queryType)
    },
    // 返回省市
    provinceCity (province, city) {
      if (
        province === '北京' ||
        province === '上海' ||
        province === '重庆' ||
        province === '天津'
      ) {
        return city
      } else {
        return province + city.substring(0, city.length)
      }
    },
    resultClick (item) {
      this.keyWord = item.title
      this.getCollegeData(this.queryType)
    },
    getResult (val = null) {
      let _list = []
      this.results = []
      if (val) {
        this.zyListData.map(item => {
          if (_list.indexOf(item) < 0 && item.indexOf(val) >= 0) {
            _list.push(item)
            this.results.push({ title: item })
          }
        })
      } else {
        this.zyListData.map(item => {
          if (_list.indexOf(item) < 0) {
            _list.push(item)
            this.results.push({ title: item })
          }
        })
      }
      _list = null
    },
    getCollegeZyRankZy () {
      let that = this
      Api.getCollegeZYRankZy().then(res => {
        if (res) {
          that.zyListData = res
          that.getResult()
        } else {
          console.log('获取学科专业排行榜所有学科失败')
        }
      })
    }
  },
  beforeDestroy () {
    this.keyWord = null
    this.levelDefaultIds = null
    this.districtDefaultIds = null
    this.departmentDefaultIds = null
    this.schoolData = null
    this.zyListData = null
    this.results = null
  }
}
